/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.framework.datasource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;


/**
 * Table model
 */
public class TableModel {
    ArrayList rows = new ArrayList();
    private int _blockSize = 5;
    private int _size = 0;
    private int _start = 1;
    private int mColumnSize = 0;

    /**
     * Creates a new TableDataObject object.
     */
    public TableModel() {
    }
    public TableModel(List names, List values) {
        rows.clear();
        mColumnSize = 0;

        for (int i = 0; i < names.size(); i++) {
            ArrayList col = new ArrayList();
            col.add(names.get(i));
            col.add(values.get(i));
            rows.add(col);
        }

        _size = rows.size();    
    }
    /**
     * Creates a new TableDataObject object.
     *
     * @param dataArray Parameter
     */
    public TableModel(String[][] dataArray) {
        rows.clear();
        mColumnSize = 0;

        for (int i = 0; i < dataArray.length; i++) {
            ArrayList col = new ArrayList();

            for (int j = 0; j < dataArray[i].length; j++) {
                col.add(dataArray[i][j]);
            }

            if (dataArray[i].length > mColumnSize) {
                mColumnSize = dataArray[i].length;
            }

            rows.add(col);
        }

        _size = rows.size();
    }

    public TableModel(String[] dataArray) {
        rows.clear();
        mColumnSize = 0;

        for (int i = 0; i < dataArray.length; i++) {
            ArrayList col = new ArrayList();

            for (int j = 0; j < 2; j++) {
                col.add(dataArray[i]);
            }

            rows.add(col);
        }

        _size = rows.size();
    }

    /**
     * Creates a new TableDataObject object.
     *
     * @param properties Parameter
     */
    public TableModel(Properties properties) {
        rows.clear();
        mColumnSize = 0;

        Enumeration enumeration = properties.propertyNames();

        while (enumeration.hasMoreElements()) {
            ArrayList col = new ArrayList();
            String key = (String) enumeration.nextElement();
            col.add(properties.get(key));
            col.add(key);
            rows.add(col);
        }

        this.sort(rows, null);
        _size = rows.size();
    }

    /**
     * Creates a new TableDataObject object.
     *
     * @param dataArray Parameter
     */
    public TableModel(List dataArray) {
        rows.clear();

        for (int i = 0; i < dataArray.size(); i++) {
            rows.add(dataArray.get(i));
        }

        _size = rows.size();
    }

    public static TableModel getTableDataObject(String[][] dataArray) {
        return new TableModel(dataArray);
    }

    /**
     * DOCUMENT ME!
     *
     * @param newValue parameter
     */
    public void setBlockSize(int newValue) {
        _blockSize = newValue;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getColumnSize() {
        return mColumnSize;
    }

    /**
     * DOCUMENT ME!
     *
     * @param i parameter
     *
     * @return return-value
     */
    public Iterator getItem(int i) {
        return ((List) rows.get((i + _start) - 1)).iterator();
    }

    public int getLength() {
        int size = _size;
        size = size - _start;

        //if larger than blocksize, then return blocksize
        return (_blockSize < size) ? _blockSize : size;
    }

    /**
     * DOCUMENT ME!
     *
     * @return return-value
     */
    public int getMaxValue() {
        return _size;
    }

    public void setValue(int newValue) {
        _start = newValue;
    }

    public void sort(List v, Comparator c) {
        if(c==null)
            c = new Comparator() {
                public int compare(Object a, Object b) {
                    String value1 = (String) ((List) a).get(0);
                    String value2 = (String) ((List) b).get(0);

                    return value1.compareTo(value2);
                };
            };    
        Collections.sort(v, c);
    }
}
